import os
from getclass import doit


def changethenumofclass():
    directory = "D:/work/images"  # 指定目录路径
    Labels = doit(directory)
    print(Labels)
    for filename in os.listdir(directory):  # 遍历目录下所有文件
        if filename[-4:] == ".txt":
            content = ""
            file_path = os.path.join(directory, filename)
            with open(file_path, 'r') as file:
                content = file.read().split()
            if len(content) == 0:
                print(filename)
                continue
            for i in Labels.keys():
                if i in filename:
                    content[0] = str(Labels[i])

            with open(file_path, 'w') as file:
                file.write(" ".join(content))
    print(Labels)


def changes():
    directory = "D:/work/images"  # 指定目录路径
    Labels = doit(directory)
    print(Labels)
    for filename in os.listdir(directory):  # 遍历目录下所有文件
        if filename[-4:] == ".txt":
            content = ""
            file_path = os.path.join(directory, filename)
            with open(file_path, 'r') as file:
                content = file.read().split()
            if len(content) == 0:
                print(filename)
                continue
            ans = [content[i*5:(i+1)*5] for i in range(len(content)//5)]
            for i in Labels.keys():
                if i in filename:
                    for j in range(len(ans)):
                        ans[j][0] = str(Labels[i])

            with open(file_path, 'w') as file:
                for i in ans:
                    file.write(" ".join(i) + '\n')
    print(Labels)

if __name__ == "__main__":
    changes()
